<?php
/**
 * Created By PhpStorm
 * Author: patrick wu
 * Date: 2020/9/24
 * Time: 17:59
 */
namespace App\Service\Admin;

use App\Http\Model\Mysql\TagModel;

class TagService extends BaseService
{
    /**
     * 标签列表
     * @return array
     */
    public function tagList()
    {
        $list   =   TagModel::query()->select(['id', 'tag_name', 'tag_desc', 'sorting'])->orderBy('sorting', 'desc')->get();
        if ($list->isEmpty()) {
            return [true, []];
        } else {
            return [true, $list->toArray()];
        }
    }

    /**
     * 标签添加
     * @param array $params  添加参数
     * @return array
     */
    public function tagAdd(array $params)
    {
        $insert['tag_name'] =   $params['tag_name'];
        $insert['tag_desc'] =   $params['tag_desc'] ?? '';
        $insert['sorting']  =   $params['sorting'];

        $insert_result  =   TagModel::create($insert);
        if ($insert_result) {
            return [true, '添加成功'];
        } else {
            return [false, '添加失败'];
        }
    }

    /**
     * 标签删除
     * @param int $tag_id
     * @return array
     */
    public function tagDelete(int $tag_id)
    {
        $delete_result  =   TagModel::where(['id' => $tag_id])->delete();
        if ($delete_result) {
            return [true, '删除成功'];
        } else {
            return [false, '删除失败'];
        }
    }


}
